package view;

import orm.MyBatisSQLSession;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MainView extends JFrame {
    public static int userId;
    public static JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 10));
    public static JPanel leftPanel = new JPanel(new BorderLayout());
    public static JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, leftPanel, rightPanel);
    public static JTextField wordTextField = new JTextField("", 15);
    public static JTextField searchField = new JTextField("", 24);
    public static JButton searchAllButton = new JButton();
    public static JCheckBox searchFavoriteCheckBox = new JCheckBox("仅搜索收藏", true);
    public static JButton randomButton = new JButton();
    public static JScrollPane scrollPanel = new JScrollPane();
    public static JPanel wordsContainer = new JPanel(new GridLayout(0, 1));
    public static JTextField meaningTextField = new JTextField("", 15);
    public static JTextField sentenceTextField = new JTextField("", 15);
    public static JTextField audioTextField = new JTextField("", 15);
    public static JButton addingButton = new JButton();
    public static JLabel hintLabel = new JLabel();
    private final String blank = "          ";
    public static Font font = new Font("等线", Font.PLAIN, 30);
    public static Font labelFont = new Font("等线", Font.PLAIN, 20);
    public static Font tipFont = new Font("宋体", Font.BOLD, 28);
    JLabel wordText = new JLabel("英语单词:" + blank);
    JLabel meaningText = new JLabel("中文释义:" + blank);
    JLabel sentenceText = new JLabel("例句:" + blank);
    JLabel audioText = new JLabel("音频文件:" + blank);

    public MainView() {
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg) {
                MyBatisSQLSession.getSqlSession().close();
            }
        });

        setLayout(new GridLayout(1, 2));
        setSize(1500, 800);
        setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        splitPane.setDividerLocation(1000);
        splitPane.setDividerSize(8);
        setContentPane(splitPane);

        searchAllButton.setIcon(new ImageIcon("icons/search.png"));
        searchAllButton.setToolTipText("搜索单词-顺序排列");
        randomButton.setIcon(new ImageIcon("icons/random.png"));
        randomButton.setToolTipText("搜索单词-随机排列");
        scrollPanel.setBorder(BorderFactory.createTitledBorder("单词列表"));
        JPanel searchPanel = new JPanel();
        searchPanel.add(searchField);
        searchField.setToolTipText("键入要搜索的单词或单词片段");
        searchPanel.add(searchAllButton);
        searchPanel.add(randomButton);
        searchPanel.add(searchFavoriteCheckBox);
        leftPanel.add(searchPanel, BorderLayout.NORTH);
        leftPanel.add(scrollPanel, BorderLayout.CENTER);

        addingButton.setIcon(new ImageIcon("icons/add.png"));
        addingButton.setToolTipText("添加单词");
        rightPanel.add(wordText);
        rightPanel.add(wordTextField);
        rightPanel.add(meaningText);
        rightPanel.add(meaningTextField);
        rightPanel.add(sentenceText);
        rightPanel.add(sentenceTextField);
        rightPanel.add(audioText);
        rightPanel.add(audioTextField);
        rightPanel.add(addingButton);
        rightPanel.add(new JLabel(blank + blank + blank + blank + blank + blank));
        rightPanel.add(hintLabel);

        setFont();
        MainViewAction.addListener();
    }

    void setFont() {
        wordText.setFont(labelFont);
        meaningText.setFont(labelFont);
        sentenceText.setFont(labelFont);
        audioText.setFont(labelFont);
        wordTextField.setFont(font);
        meaningTextField.setFont(font);
        sentenceTextField.setFont(font);
        audioTextField.setFont(font);
        addingButton.setFont(font);
        hintLabel.setFont(tipFont);
        hintLabel.setForeground(Color.red);
        searchField.setFont(font);
        searchFavoriteCheckBox.setFont(font);
    }
}